import { getListData } from '@/api/service/testApi.js'

export function useListPage() {
  const loading = ref(false)
  const finished = ref(false)
  let start = 1 // 起始页
  const length = 10 // 每页个数
  let totalNum = 1 // 总个数
  const list = ref([]) // 所有的结果都放在这个数组里，翻页后往里插入

  const onLoad = () => {
    return new Promise((resolve, reject) => {
      getListData({
        pageNo: start,
        pageSize: length,
      }).then((res) => {
        list.value = [...list.value, ...res.result.content]
        totalNum = res.result.totalElements
        start += 1

        loading.value = false // 加载状态结束
        if (list.value.length >= totalNum) {
          finished.value = true // 数据全部加载完成
        }
      })
    })
  }

  return {
    loading,
    finished,
    list,
    onLoad,
  }
}
